package com.chx.three.mama.view;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import com.chx.three.mama.R;
import com.chx.three.mama.utils.HttpTool;
import com.chx.three.mama.utils.TimeHelper;


/**
 * 检查网络是否启动的图片布局
 */
public class CheckNetImageView extends ImageView {
    Context mContext;
    private AnimationDrawable drawable;
    //上次点击事件，用来判断双击
    private long lastClickTime;
    //点击间隔1000毫秒视为双击
    private int clickDaily=1000;
    private OnDoubleClickListener onDoubleClickListener;
    //判断是否有网络
    private boolean isFlying;
    Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    //检查网络
                    checkNet();
                    break;
            }
        }
    };
    public CheckNetImageView(Context context) {
        this(context,null);
    }

    public CheckNetImageView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CheckNetImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext=context;
        setBackgroundColor(getResources().getColor(R.color.white));
        setScaleType(ScaleType.CENTER_INSIDE);
        setFly();
        checkNet();
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                    long currentTime= TimeHelper.getInstance().getCurrentMillisTime();
                    if(currentTime>(lastClickTime+1000)){
                        lastClickTime=currentTime;
                        return;
                    }
                    lastClickTime=currentTime;
                    if(!isFlying){
                        refly();
                    }
                    if(onDoubleClickListener!=null){
                        onDoubleClickListener.onDoubleClick();
                    }
            }
        }) ;

    }

    private void checkNet() {
        boolean networkAvailable = HttpTool.isNetworkAvailable(mContext);
        if (!networkAvailable) {
            drawable.stop();
            setImageResource(R.drawable.reload);
            isFlying = false;
        }
    }
    public void setFly(){
        setImageResource(R.drawable.anim_loading);
        drawable = (AnimationDrawable) getDrawable();
        drawable.start();
        isFlying=true;
        //当加载时每两秒加载检查一次网络
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (isFlying){
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    mHandler.sendEmptyMessage(1);
                }
            }
        }).start();
    }
    /**
     * 重新检查网络
     */
    public void refly(){
        setFly();
        mHandler.sendEmptyMessageDelayed(1,1000);
    }


    /**
     *当网络请求完成时，取消动画，隐藏自己
     */
    public void onResponse(){
        drawable.stop();
        setVisibility(View.GONE);
    }
    /**
     *当网络请求完成时，取消动画，隐藏自己
     */
    public void onError(){
        drawable.stop();
        setImageResource(R.drawable.reload);
        isFlying=false;
    }
    public void setOnDoubleClickListener(OnDoubleClickListener onDoubleClickListener) {
        this.onDoubleClickListener = onDoubleClickListener;
    }

    //双击时连接网络的接口
    public interface OnDoubleClickListener{
        void onDoubleClick();
    }
}
